<!--
  Created by IntelliJ IDEA.
  User: Dai
  Date: 2018/4/11
  Time: 10:51
  Remark: 工程配置 -  对象管理 -  项目管理 - 编辑设备树 - 批量增加变流器和电池包
  To change this template use File | Settings | File Templates.
-->
<!--标题栏-->
<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    <h4 class="modal-title">批量生成电池包和变流器</h4>
</div>
<!--表格栏-->
<div class="modal-body">
    <div style="width: 100%;height: 200px;">
        <div style="margin-top: 10px; height: 40px">
            <div class="col-sm-7">
                <label>变流器命名：</label>
                <input type="text" style="width: 40px;" disabled>
                <span>流水号</span>
                <input type="text" value="#变流器(PCS)" style="width: 120px" id="pcs_name">
            </div>
            <div class="col-sm-5">
                <label>流水号范围：</label>
                <input type="number" value="1" style="width: 40px" id="pcs_low">
                <span>~</span>
                <input type="number" value="3" style="width: 40px" id="pcs_high">
            </div>
        </div>
        <div style="margin-top: 10px; height: 40px">
            <div class="col-sm-7">
                <label>电池包命名：</label>
                <input type="text" style="width: 40px;" disabled>
                <span>流水号</span>
                <input type="text" value="#电池包(BMS)" style="width: 120px" id="bms_name">
            </div>
            <div class="col-sm-5">
                <label>流水号范围：</label>
                <input type="number" value="1"style="width: 40px" id="bms_low">
                <span>~</span>
                <input type="number" value="3"style="width: 40px" id="bms_high">
            </div>
        </div>
    </div>
</div>
<!--按钮栏-->
<div class="modal-footer">
    <button type="button" class="btn btn-default" onclick="check_info(this)">确定</button>
</div>
<!--该页面的js 未完成部分见TODO -->
<script>
    /**
     * 检查填写的信息是否完整
     */
    function check_info(button) {
        var pcs_name = $("#pcs_name").val().trim();
        var pcs_low = $("#pcs_low").val().trim();
        var pcs_high = $("#pcs_high").val().trim();
        var bms_name = $("#bms_name").val().trim();
        var bms_low = $("#bms_low").val().trim();
        var bms_high = $("#bms_high").val().trim();
        if (pcs_name == "") {
            alert("变流器命名不能为空");
            return false;
        } else if (bms_name == "") {
            alert("电池包命名不能为空")
            return false;
        } else if (pcs_low > pcs_high || bms_low > bms_high) {
            alert("流水号填写错误");
            return false;
        }
        var project_id = $("#tree_id").val();
        //检测命名重复
        for (var i = 0; i < zNodes.length; i++) {
            var name = zNodes[i].name;
            var type = zNodes[i].type;
            if (type == "1") {
                console.log(name + "\t" + type);
                // 新建变流包
                for (var j = pcs_low; j <= pcs_high; j++) {
                    var newName = j + pcs_name;
                    if (newName == name) {
                        alert("命名重复，无法新增");
                        return false;
                    }
                }
            } else if (type == "2") {
                console.log(name + "\t" + type);
                // 新建电池包
                for (var j = bms_low; j <= bms_high; j++) {
                    var newName = j + bms_name;
                    if (newName == name) {
                        alert("命名重复，无法新增");
                        return false;
                    }
                }
            }
        }
        $.post(
            '/project/objectManage/save_equipment_list',
            {
                project_id: $("#tree_id").val(),
                pcs_name: pcs_name, pcs_low: pcs_low, pcs_high: pcs_high,
                bms_name: bms_name, bms_low: bms_low, bms_high: bms_high
            },
            function (result) {
                zNodes = result;
                var treeObj = $.fn.zTree.init($("#treeDemo"), setting, zNodes);
                treeObj.expandAll(true);    // 设置默认展开
                // 关闭弹框
                $(button).parent().siblings(":first").find("button").click();
            }
        )
    }
</script>